import json

import pygal_maps_world.maps
from pygal.style import RotateStyle
from c_16.country_codes import get_country_code

filename = 'D:\\Python\\projects\\python_01\\files\\gdp_json.json'
with open(filename) as f:
    pop_data = json.load(f)

# 创建一个包含人口数量的字典
cc_gdp = {}
for gdp_dict in pop_data:
    if gdp_dict['Year'] == 2016:
        country = gdp_dict['Country Name']
        gdp = int(float(gdp_dict['Value']))
        code = get_country_code(country)
        if code:
            cc_gdp[code] = gdp

cc_gdp1, cc_gdp2, cc_gdp3 = {}, {}, {}
for cc, gdp in cc_gdp.items():
    if gdp < 10000000000:
        cc_gdp1[cc] = gdp
    elif gdp < 50000000000:
        cc_gdp2[cc] = gdp
    else:
        cc_gdp3[cc] = gdp

wm_style = RotateStyle('#00FFFF')
wm = pygal_maps_world.maps.World(style = wm_style)
wm.title = 'World GDP in 2016, by Country'
wm.add('0-10bn', cc_gdp1)
wm.add('10bn-500bn', cc_gdp2)
wm.add('>500bn', cc_gdp3)
wm.render_to_file('world_gdp_2016.svg')